<?php
/**
 * @copyright: ©2024 永州 微聚 商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */
namespace app\api\controller\consume;
use app\admin\model\ActivityConsumer;
use app\common\controller\AdminController;
use think\App;
use think\Db;

/**
 * @ControllerAnnotation(title="会员管理")
 */
class Classify extends AdminController
{
    
    use \app\admin\traits\Curd;

    /**
     * 字段排序
     *
     * @var array
     */
    protected $sort = [
        'sort' => 'desc'
    ];
    public function __construct(App $app)
    {
        parent::__construct($app);
        
        $this->model = new \app\admin\model\Classify();
    }
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where = [];
        if ($this->request->platform_id >0 ){
            $where[] = ['platform_id','=',$this->request->platform_id];
        }
        if (isset( $param['name']) &&   $param['name']){
            $where[] = ['name','like',"%{$param['name']}%"];
        }
        if (isset( $param['telphone']) &&   $param['telphone'] ){
            $where[] = ['platformInfo.telphone','=',$param['telphone']];
        }
        $count = $this->model->withJoin('platformInfo', 'LEFT')
            ->where($where)
            ->count();
        $list = $this->model->withJoin('platformInfo', 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();
        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }

    public function add(){
        $request = $this->request;
        $param = $request->param();
        if (empty($param['name'])){
            return  msg([], 1, "请输入分类名称") ;
        }
        if (empty($param['img'])){
            return  msg([], 1, "请上传分类图片") ;
        }
        $data = [
            'platform_id' => $this->request->platform_id,
            'name' => $param['name'],
            'img' => $param['img'],
            'status' => $param['status'],
            'sort' => $param['sort'],
        ];

        $this->model->save($data);
        return msg([], 0, "添加成功!");
    }

    public function edit(){
        $request = $this->request;
        $param = $request->param();
        $data = [
            'name' => $param['name'],
            'img' => $param['img'],
            'status' => $param['status'],
            'sort' => $param['sort'],
        ];
        $this->model->where(['id' =>$param['id'] ])->update($data);
        return msg([], 0, "添加成功!");
    }




    public function sortChange()
    {
        $request = $this->request;

        $this->model->startTrans();
        if ($this->request->isPost()) {
            $post = $this->request->post();
            try {
                $data = $post['params'];
                $sort_number = count($data);
                foreach ($data as $k => $v) {
                    $this->model->where("id", $v['id'])->update(array(
                        "sort" => $sort_number
                    ));
                    $sort_number --;
                }

                $this->model->commit();
            } catch (\Exception $e) {
                $this->model->rollback();
                return msg([], 1, "保存失败" . $e->getMessage());
            }
            return msg([], 0, "保存成功");
        }
    }


}